﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AccessGrant.GrantFrameworks;
using FAQFrameworks;
using FooFun.DataElements.Dtos;

namespace DTOFrameworks
{
    public class LoginUser
    {
        PermissionQuery query;
        RoleSetQuery roleQuery;
        FAQContext context;

        public LoginUser()
        {
            query = PermissionQuery.Create();
            context = FAQContext.Current;

            roleQuery = RoleSetQuery.Create();
        }

        public Guid AccountId { get; set; }

        public bool IsInRole(IEnumItem role)
        {
            IDataElement de
                = roleQuery.GetRoleOfUserInForum(this.AccountId, context.ForumId);
            if (de["RoleId"].Equals(role.Id))
            {
                return true;
            }

            return false;
        }

        public bool IsInRole(string role)
        {
            IEnumItem roleItem = query.GetAccountByName(role);
            return IsInRole(roleItem);
        }

        public override bool Equals(object obj)
        {
            if (!(obj is LoginUser) || obj == null)
            {
                return false;
            }

            LoginUser user = obj as LoginUser;

            return user.AccountId == this.AccountId;
        }

        public override int GetHashCode()
        {
            return this.AccountId.GetHashCode();
        }

        public static LoginUser AnonymousUser
        {
            get
            {
                LoginUser user = new LoginUser();
                user.AccountId = Guid.Empty;

                return user;
            }
        }
    }
}
